#!/usr/bin/node

const log = console.log,
      err = console.error;

const http = require('http'),
      fs = require('fs'),
      path = require('path'),
      file = process.argv[2];

if(process.argv.length!==3){
  err('Command line parameters: cmd fileName');
  process.exit(1);
}

try{
  var data = fs.readFileSync(file);
  var uri = data.toString('base64');
}catch(e){
  err(e.message);
  process.exit(2);
}

log(`uri:${uri}`);

let ext = path.extname(file);

let html = `<html><head><title>image</title></head><body><img src="data:image/${ext.slice(1,ext.length)};base64,${uri}"/></body></html>`;

http.createServer((req,res)=>{
  //log(req.headers);
  //log(req.url);
  res.end(html);
}).listen(8080);
